{!!'<?php'!!}
namespace {{$namesPace}};
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ExcelExport;
use App\Http\Controllers\Controller;
use App\Utils\ResponseUtil;
use App\DTOS\Admin\{{$config['modelName']}}SearchDTO;
@foreach($methods as $method)
@if($method==='paginate')
use App\Services\{{$config['modelName']}}Service;
@elseif($method==='import')
@elseif($method==='export')
@elseif($method==='insert')
use App\DTOS\Admin\{{$config['modelName']}}InsertDTO;
@elseif($method==='update')
use App\DTOS\Admin\{{$config['modelName']}}UpdateDTO;
@endif
@endforeach
// {{$config['modelShowName']}}
class {{$className}} extends Controller
{
    private {{$config['modelName']}}Service $service;

    /**
    * {{$config['modelName']}}Service constructor.
    * @param {{$config['modelName']}}Service $service
    */
    public function __construct({{$config['modelName']}}Service $service)
    {
        $this->service = $service;
    }

@foreach($methods as $method)
@if($method==='paginate')

    public function paginate()
    {
        $DTO = new {{$config['modelName']}}SearchDTO(request()->all());
        $paginate = $this->service->paginate($DTO);
        $data = $paginate->items();

        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }

@elseif($method==='import')
    public function import()
    {
        $file = request()->file('file');
        $sheets = Excel::toArray(null,$file);
        $sheet = $sheets[0];
        $inserts = [];
        foreach($sheet as $key=>$arr) {
            if ($key>1){
                $row = [
                    'column_1'=>$arr[1],
                    'column_2'=>$arr[2],
                ];
                $inserts[] = $row;
            }
        }
        var_dump($inserts);die;//return ResponseUtil::success();
    }

@elseif($method==='export')
    public function export()
    {
        $DTO = new {{$config['modelName']}}SearchDTO(request()->all());
        $paginate = $this->service->paginate($DTO);
        $excelRows = [
            ['ID','创建时间']
        ];
        foreach($paginate->items() as $datum){
            $excelRows[] = [ $datum['id'], $datum['created_at'] ];
        }
        $excel = new ExcelExport($excelRows);
        return Excel::download($excel, '{{$config['modelShowName']}}.xlsx');
    }
@elseif($method==='insert')
    public function insert()
    {
        try {
            $DTO = new {{$config['modelName']}}InsertDTO(request()->post());
            $this->service->insert($DTO);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }
@elseif($method==='update')
    public function update($id)
    {
        try{

            $DTO = new {{$config['modelName']}}UpdateDTO(request()->post());
            $this->service->updateById($id,$DTO);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

@elseif($method==='detail')
    public function detail($id)
    {
        $data = $this->service->getById($id);
        return ResponseUtil::success($data);
    }

@elseif($method==='delete')
    public function delete($id){
        try{
            $this->service->deleteById($id);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }
@else
    public function {{$method}}(){
         return  null;
    }

@endif
@endforeach
}
